var express = require('express');
var router = express.Router();
var { Movie, Myuser, Vuereg, Vuedata, Jiajia } = require("../utils/model");
var { createToken } = require("../utils/token")
router.get("/vue", (req, res) => {
    res.json({
        code: 200,
        msg: "连接成功",
        result: []
    })
})

router.get("/vuelist", (req, res) => {
    Movie.find({}).then(result => {
        res.json({
            result
        })
    })
})

router.post("/mylogin", (req, res) => {
    var body = req.body;
    Myuser.findOne({ username: body.username }).then(result => {
        if (result) {
            if (result.userpwd == body.pwd) {
                res.json({
                    msg: "登录成功",
                    code: 200,
                    result
                })
            } else {
                console.log("error")
            }
        } else {
            console.log("用户名密码不正确")
        }
    })
})
// vue项目注册
router.post("/signup", (req, res) => {
    var body = req.body;
    Vuereg.findOne({
        phone: body.phone
    }).then(data => {
        if (data) {
            res.json({
                code: 200,
                msg: "该用户已被注册",
                type: 0
            })
        } else {
            body.time = new Date(),
                Vuereg.insertMany(body).then(result => {
                    res.json({
                        code: 200,
                        msg: "注册成功",
                        type: 1
                    })
                }).catch(err => {
                    res.json({
                        code: 500,
                        msg: "服务器异常",
                        type: 0
                    })
                })
        }
    })
})



//登录
router.post("/signin", (req, res) => {
    var body = req.body;
    Vuereg.findOne({ phone: body.phone }).then(result => {
        let token = createToken({ phone: body.phone })
        if (result) {
            res.json({
                code: 200,
                msg: "登录成功",
                type: 1,
                username: body.phone,
                token
            })
        } else {
            console.log("手机号不正确")
        }
    })
})


//图片上传
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cd) {
        cd(null, './public/avatar') //设置图片存放的路径
    },
    filename: function (req, file, cd) {
        cd(null, Date.now() + "wh2006" + file.originalname)
    }
})
var upload = multer({ storage: storage }).any();  // 可以上传任何格式的图片

router.post("/uploadavatarAjax", upload, (req, res) => {
    console.log(req.files)
    if (req.files) {
        var avatar = req.files[0].path;
        Vuereg.updateMany({}, { $set: { avatar: avatar } }).then(result => {
            res.json({
                code: 200,
                msg: "上传成功",
                type: 1,
                result,
                avatar
            })
        })
    } else {
        res.json({
            code: 500,
            msg: "上传失败",
            type: 0,
        })
    }
})

//登录/注册
router.post('/postlogin', (req, res) => {
    var body = req.body
    console.log(body)
    Vuereg.findOne({ phone: body.phone }).then(result => {
        if (result) {
            res.json({
                msg: "登录成功",
                code: 200,
                type: 0,
                phone: body.phone,
                time: new Date()
            })
        } else {
            body.time = new Date(),
                Vuereg.insertMany(body).then(result => {
                    res.json({
                        code: 200,
                        msg: "注册成功",
                        type: 1
                    })
                }).catch(err => {
                    res.json({
                        code: 500,
                        msg: "服务器异常",
                        type: 0
                    })
                })
        }
    })
})

//录入数据
router.get('/luru', (req, res) => {
    var body = req.query
    console.log(body)
    Vuedata.findOne({ _id: body._id }).then(result => {
        if (result) {
            Vuedata.updateOne({ _id: body._id }, { $inc: { num: body.num } }).then(result => {
                res.json({
                    msg: "成功",
                    type: 0,
                    result,
                })
            })
        } else {
            Vuedata.insertMany(body).then(result => {
                res.json({
                    msg: "添加成功",
                    type: 0,
                    result,
                })
            })
        }
    })
})

router.get('/jiaju', (req, res) => {
    var body = req.query
    Jiajia.insertMany(body).then(result => {
        res.json({
            msg: "添加成功",
            type: 0,
            result,
        })
    })
})

router.get('/findjia', (req, res) => {
    Jiajia.find().then(result => {
        res.json({
            msg: "查询成功",
            type: 0,
            result,
        })
    })
})

router.get('/setdata', (req, res) => {
    Vuedata.find({}).then(result => {
        res.json({
            msg: "查询成功",
            type: 0,
            result,
        })
    })
})

router.get('/del', (req, res) => {
    var body = req.query
    Vuedata.find({ _id: body.id }).then(result => {
        if (result) {
            Vuedata.deleteOne({ _id: body.id }).then(result => {
                if (result) {
                    res.json({
                        msg: "删除成功",
                        type: 0,
                    })
                } else {
                    res.json({
                        msg: "删除失败",
                        type: 1,
                    })
                }
            })
        }
    })
})


module.exports = router